import axios from 'axios'
import ENV from './ENV'
import QS from 'qs'

function ImgFactory() {
  // this.arr= arguments[0] //

  this.arr = {}
  this.form = new FormData()
  this.request = axios.create({
    // withCredentials: true,
    headers: {
      'Content-Type': 'multipart/form-data'
      // 'Access-Control-Allow-Origin':'*'
    }
  })

}

//getimage
// http://image.myjinyitech.com/getimage?pathname=Tracing/6/certificate/3zvH5XqU4BZxEUlSuK8GNzLjfPV3rp8FtQb65fx7.png&height=150
ImgFactory.prototype.seend = function(parameter) { //入口 1验证参数是否合法 绑定属性
                                                   // Object.prototype.toString.call(parameter)    //判断类型  是否是JSON
  let flag = Object.prototype.toString.call(parameter).split(/[\s\n]/)[1].split(']')[0]
  if (flag != 'Object') {
    return false
  }
  this.arr = parameter
  return this

}
ImgFactory.prototype._addimg = function() {//添加图片 获取参数 AXIOS
  this.form.append('pathname', this.arr.path)
  this.form.append('image', this.arr.file)
  if (this.arr.path) {
    return this.request({
      method: 'post',
      url: ENV.API_CONFIG.ADDIMG,
      data: this.form
    })
  } else {
    console.log('err')
  }
}
ImgFactory.prototype._delete = function() { //删除文件夹里面所有图片
  if (this.arr.path) {
    return this.request.get(ENV.API_CONFIG.deletedir + '?pathname=' + this.arr.path)
  }
}
ImgFactory.prototype._deleteimg = function() { //删除指定图片
  if (this.arr.pathimg) {
    return this.request.get(ENV.API_CONFIG.deleteimage + '?pathname=' + this.arr.pathimg)
  }
}
ImgFactory.prototype._getimage = function() {  //根据图片获取
  // Tracing/1/IDPositiv2/QT4IeGAydgfnqMFpEQhhZoTuS4vqnUixKukZNM3k.png
  this.request.get(ENV.API_CONFIG.getimage + '?pathname=' + this.arr.path).then((ref) => {
    let flag = JSON.parse(ref.request.response)
    if (flag.code == 200) {
      return JSON.parse(ref.request.response)
    }
  }).catch((res) => {

  })
}
ImgFactory.prototype._select = function() {  //根据文件夹查找图片
  return this.request.get(ENV.API_CONFIG.searchimages + '?pathname=' + this.arr.path)
}

//动作
ImgFactory.prototype.update = function(callbackA,callbackB) {  //修改文件夹所有图片
  this._select().then((ref) => {
    if (ref.status == 200 && ref.data.length != 0) { //有数据
         this._delete().then((deleref) => {
        let flag = JSON.parse(deleref.request.response);
        if (flag.code == 200) {
         this._addimg().then(callbackA,callbackB);
        }
      }).catch((deres) => {
      })
    } else {                                     //没有数据
      this._addimg().then(callbackA,callbackB);
    }
  }).catch((res) => {
  })
}

ImgFactory.prototype.updateimage = function(callbackA,callbackB) {  //修改指定图片
  this._select().then((ref) => {
    if (ref.status == 200 && ref.data.length != 0) { //有数据
      this._deleteimg().then((deleref) => {
        let flag = JSON.parse(deleref.request.response);
        if (flag.code == 200) {
          this._addimg().then(callbackA,callbackB);
        }
      }).catch((deres) => {
        this._addimg().then(callbackA,callbackB);
      })
    } else {  //没有数据
      this._addimg().then(callbackA,callbackB);
    }
  }).catch((res) => {
  })
}
ImgFactory.prototype.addimg = function(CallbackA,CallbackB) { //添加图片
  this._addimg().then(CallbackA,CallbackB);
}
ImgFactory.prototype.deleteimg = function(CallbackA,CallbackB) { //删除图片
  this._deleteimg().then(CallbackA,CallbackB);
}
ImgFactory.prototype.select = function(CallbackA,CallbackB) { //删除文件夹里面所有图片
  this._select().then(CallbackA,CallbackB);
}
ImgFactory.prototype.selectimg = function(CallbackA,CallbackB) { //查询图片
  this._getimage().then(CallbackA,CallbackB);
}

export default ImgFactory
